package com.example.sunmiv1.utils;


import android.graphics.Bitmap;
import android.widget.ImageView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.util.Hashtable;

/**
 * 二維碼生成
 */

public class QR_code {
    private static Bitmap bitmap;

    private static final int BLACK = 0xff000000;
    private static final int WHITE = 0xFFFFFFFF;
    private static BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128;

    /**
     * 生成二維碼的方法
     *
     * @param address
     * @return
     */
    public static Bitmap createQRImage(String address, ImageView iv_qr_code) {
        int QR_WIDTH = 200, QR_HEIGHT = 200;
        //判斷URL合法性
        if (address == null || "".equals(address) || address.length() < 1) {
            return null;
        }
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        //圖像數據轉換，使用了矩陣轉換
        BitMatrix bitMatrix = null;
        try {
            bitMatrix = new QRCodeWriter().encode(address, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
        //下面這裏按照二維碼的算法，逐個生成二維碼的圖片，
        //兩個for循環是圖片橫列掃描的結果
        for (int y = 0; y < QR_HEIGHT; y++) {
            for (int x = 0; x < QR_WIDTH; x++) {
                if (bitMatrix.get(x, y)) {
                    pixels[y * QR_WIDTH + x] = 0xff000000;
                } else {
                    pixels[y * QR_WIDTH + x] = 0xffffffff;
                }
            }
        }
        //生成二維碼圖片的格式，使用ARGB_8888
        bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
        //顯示到一個ImageView上面
        iv_qr_code.setImageBitmap(bitmap);
        return bitmap;
    }

    /*生成條形碼*/
    public static Bitmap createBarcode(String contents, int desiredWidth, int desiredHeight) {
        MultiFormatWriter writer = new MultiFormatWriter();
        BitMatrix result = null;
        try {
            result = writer.encode(contents, barcodeFormat, desiredWidth,
                    desiredHeight);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        int width = result.getWidth();
        int height = result.getHeight();
        int[] pixels = new int[width * height];
        // All are 0, or black, by default
        for (int y = 0; y < height; y++) {
            int offset = y * width;
            for (int x = 0; x < width; x++) {
                pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }

    /*生成二維碼*/
    public static Bitmap createQRCode(String content, int w, int h/*,String filePath*/) {
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        //圖像數據轉換，使用了矩陣轉換
        BitMatrix bitMatrix = null;
        try {
            bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, w, h, hints);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        int[] pixels = new int[w * h];
        //下面這裏按照二維碼的算法，逐個生成二維碼的圖片，
        //兩個for循環是圖片橫列掃描的結果
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                if (bitMatrix.get(x, y)) {
                    pixels[y * w + x] = 0xff000000;
                } else {
                    pixels[y * w + x] = 0xffffffff;
                }
            }
        }
        //生成二維碼圖片的格式，使用ARGB_8888
        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, w, 0, 0, w, h);

        //必須使用compress方法將bitmap保存到文件中再進行讀取。直接返回的bitmap是沒有任何壓縮的，內存消耗巨大！
        /*try {
            return bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(filePath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }*/
        return bitmap;

    }
}
